//
//  LoginController.m
//  village_ios
//
//  Created by mac on 2019/8/11.
//  Copyright © 2019 com.sw.village. All rights reserved.
//

#import "RegisterController.h"
#import "RegisterRequest.h"
#import "UIAlertController+Blocks.h"
#import "SSPhotoPickerManager.h"
#import "RegisterPhoneCodeController.h"

typedef NS_ENUM(NSInteger,CardType) {
    CardTypeUp = 11,
    CardTypeDown = 12
};

@interface RegisterController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *apartmentTextField;

@property (weak, nonatomic) IBOutlet UIImageView *upCardImageView;
@property (weak, nonatomic) IBOutlet UIImageView *downCardImageView;

@end

@implementation RegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupImageView];
}

- (IBAction)toChooseApartment:(id)sender {
}


#pragma mark - uploadImageView
- (void)setupImageView {
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toUpCardImageView)];
    [self.upCardImageView addGestureRecognizer:tap1];
    self.upCardImageView.tag  = CardTypeUp;
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toDownCardImageView)];
    [self.downCardImageView addGestureRecognizer:tap2];
    self.upCardImageView.tag  = CardTypeDown;


}
- (void)toUpload:(NSInteger)tag {

    [UIAlertController showAlertInViewController:self withTitle:@"选择图片" message:nil alertControllerStyle:UIAlertControllerStyleActionSheet cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"相册",@"拍照"] tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
        if (buttonIndex != 0) {

            UIImagePickerControllerSourceType type = (buttonIndex == 2) ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
            [[SSPhotoPickerManager sharedSSPhotoPickerManager]showOnPickerViewControllerSourceType:type onViewController:self compled:^(UIImage *image, NSDictionary *editingInfo) {

                [SYProgressHUD showLoadingWindowText:@"正在上传"];
                [APIManager uploadImageToServerWithImage:image type:@"normal" success:^(id response) {
                        if(tag == CardTypeUp) {
                            self.upCardImageView.image = image;
                            self.registerRequest.upcardURL = response[@"url"];
                        } else if(tag == CardTypeDown) {
                            self.downCardImageView.image = image;
                            self.registerRequest.downcardURL = response[@"url"];
                        }

                } failure:^(NSError *error) {
                    [SYProgressHUD showFailureText:@"上传失败"];

                }];
            } cancel:^{

                [SYProgressHUD hide];


            }];
        }

    }];
}
- (void)toUpCardImageView {
    [self toUpload:CardTypeUp];
}

- (void)toDownCardImageView {
    [self toUpload:CardTypeDown];
}
- (IBAction)toNext:(id)sender {

    self.registerRequest = [RegisterRequest new];
    self.registerRequest.name = self.nameTextField.text;
    self.registerRequest.phone = self.phoneTextField.text;
    self.registerRequest.address = self.addressTextField.text;
    self.registerRequest.apartment = self.apartmentTextField.text;
//    self.registerRequest.upcardURL = self.nameTextField.text;
//    self.registerRequest.downcardURL = self.nameTextField.text;

    RegisterPhoneCodeController *vc = [RegisterPhoneCodeController instanceWithStoryBoard];
    vc.registerRequest = self.registerRequest;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
